package com.xiaoping.recyclerview4;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<String> mTitle = null;
    private OnClickItemListener mOnClickItemListener;

    public MyAdapter(Context context, List<String> title, OnClickItemListener listener) {
        this.mContext = context;
        this.mTitle = title;
        this.mOnClickItemListener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        //定义view
        private TextView mTextView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //获取view
            mTextView = itemView.findViewById(R.id.item_tv);
        }
    }

    public class MyViewHolder1 extends RecyclerView.ViewHolder{
        //定义view
        private Button mButton;
        public MyViewHolder1(@NonNull View itemView) {
            super(itemView);
            //获取view
            mButton = itemView.findViewById(R.id.item_btn);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_0, parent, false));
        } else {
            return new MyViewHolder1(LayoutInflater.from(mContext).inflate(R.layout.item_1, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            ((MyViewHolder)holder).mTextView.setText("text" + mTitle.get(position));
            ((MyViewHolder)holder).mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickItemListener.onClickItemListener(position);
                }
            });
        } else if (getItemViewType(position) == 1) {
            ((MyViewHolder1)holder).mButton.setText("btn" + mTitle.get(position));
            ((MyViewHolder1)holder).mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnClickItemListener.onClickItemListener(position);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {//onCreateViewHolder里是viewType
        if (position == 3) {
            return 1;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return mTitle == null ? 0 : mTitle.size();
    }

    public interface OnClickItemListener {
        void onClickItemListener(int postion);
    }
}
